﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_ScansiaPacific
{
    public partial class FrKH_ChiTietThieu : DevComponents.DotNetBar.Office2007Form
    {
        private string MaKH;
        public FrKH_ChiTietThieu()
        {
            InitializeComponent();
        }

        private void FrKHMuaGo_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns["MToi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["M3"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView2.Columns["MToiCan"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView2.Columns["Thanh"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            
           
        }
        private void ChiTietThieu()
        {
            try
            {
                Process prs = new Process();
                MaKH = txt_MaKH.Text;
                string select = @"select qry1.TenSP,CTSP.MaChiTiet,CTSP.TenChiTiet,((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(0,SL.SoLuong))*CTSP.ChieuDaiSC as MToi,((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(0,SL.SoLuong))*(CTSP.ChieuDaySC*CTSP.ChieuRongSC*CTSP.ChieuDaiSC)/1000000000 as M3
                                from (select SanPham.MaSP,SanPham.TenSP,KeHoachSP.SoLuongSP from dbo.KeHoachSP inner join dbo.SanPham on SanPham.MaSP=KeHoachSP.MaSP where MaKeHoach='" + MaKH;
                select += @"') as qry1 inner join dbo.ChiTietSP as CTSP
                                on qry1.MaSP=CTSP.MaSP left join dbo.KH_SLCT as SL on SL.MaChiTiet=CTSP.MaChiTiet";
                DataSet ds = prs.dts(select);
                dataGridView1.DataSource = ds.Tables[0];
                ds.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void QuyCachThieu()
        {
            try
            {
                Process prs = new Process();
                MaKH = txt_MaKH.Text;
                FormProcess Frp = new FormProcess();
                if (Frp.isfloat(txt_DM.Text))
                {
                    float i = float.Parse(txt_DM.Text);
                    string select = @"select out.ChieuDaySC,out.ChieuRongSC,sum(out.MToi) as MToi,out.Long,convert(decimal(10,2),sum(out.MToi)/(1200*"+i;
                            select+= @")) as SLThanh
                                    from (select CTSP.ChieuDaySC,CTSP.ChieuRongSC,CTSP.MaChiTiet,CTSP.TenChiTiet,CTSP.Long,((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(0,SL.SoLuong))*CTSP.ChieuDaiSC as MToi
                                    from (select SanPham.MaSP,SanPham.TenSP,KeHoachSP.SoLuongSP from dbo.KeHoachSP inner join dbo.SanPham on SanPham.MaSP=KeHoachSP.MaSP where MaKeHoach='" + MaKH;
                            select+= @"') as qry1 inner join dbo.ChiTietSP as CTSP
                                    on qry1.MaSP=CTSP.MaSP left join dbo.KH_SLCT as SL on SL.MaChiTiet=CTSP.MaChiTiet
								    where ((qry1.SoLuongSP)*(CTSP.SoLuongCT)-isnull(0,SL.SoLuong))>0) as out
                                    group by out.ChieuDaySC,out.ChieuRongSC,out.Long";
                    DataSet ds = prs.dts(select);
                    dataGridView2.DataSource = ds.Tables[0];
                    ds.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                float i = float.Parse(txt_DM.Text);
                if (i > 0 && i < 1)
                {
                    ChiTietThieu();
                    QuyCachThieu();
                }
                else
                    MessageBox.Show("Giá trị nhập 0<x<1");
            }
            catch (Exception ex)
            {
                string s = "Input string";
                if (ex.Message.ToString().Contains(s))
                {
                    MessageBox.Show("Giá trị nhập phải là số");
                    txt_DM.Text = "";
                }
                else
                    MessageBox.Show(ex.Message);
            }
        }

       
    }
}
